﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;


namespace Applib.AppCompatibility
{
    public static class AppEnv
    {
        #region get browser version functions
        private const string BrowserInstallerRegKey = @"SOFTWARE\Clients\StartMenuInternet\";
        public enum BrowserNames
        {
            IExplorer,
            Firefox,
            Chrome,
            Unknown
        }
        private static readonly Dictionary<BrowserNames, string> browserNameDict = new Dictionary<BrowserNames, string>()
            {
                {BrowserNames.IExplorer, "IEXPLORE.EXE" },
                {BrowserNames.Firefox, "FIREFOX.EXE" },
                 {BrowserNames.Chrome, "GOOGLE CHROME"},
                  {BrowserNames.Unknown, "Unknown"}
            };

        public static string GetBrowserVersion(BrowserNames bname)
        {
            string productVersion = "0";
            string browserCommandRegKey = string.Concat(BrowserInstallerRegKey, @"\shell\open\command");

            string browserName = browserNameDict[bname];


            RegistryKey root = Registry.LocalMachine.OpenSubKey(BrowserInstallerRegKey, false);
            RegistryKey key = null;
            //name could be localized, find partial match; also test will not run as wow32, so ignore this one.
            foreach (string name in root.GetSubKeyNames())
            {
                if (name.ToUpper().Contains(browserName))
                {
                    key = root.OpenSubKey(string.Concat(name, @"\shell\open\command"));
                    break;
                }
            }

            if (key != null)
            {
                string file = key.GetValue("", "c:\notexist.txt").ToString().Replace("\"", "");

                if (System.IO.File.Exists(file))
                {
                    FileVersionInfo fver = FileVersionInfo.GetVersionInfo(file);
                    Version ver = new Version(fver.ProductVersion);
                    productVersion = ver.Major.ToString();
                }
            }

            return productVersion;

        }

        /// <summary>
        /// Get browser name saved in registry. HKCKU\Software\Clients\StartmenuInternet
        /// </summary>
        /// <returns></returns>
        public static BrowserNames GetDefaultBrowserName()
        {
            const string defaultBrowserPath = @"Software\Clients\StartmenuInternet";
            RegistryKey usercfg = Registry.CurrentUser.OpenSubKey(defaultBrowserPath, false);
            //if this key is null, consider default is IE
            string defaultName = usercfg != null ? usercfg.GetValue("", "").ToString() : "IEXPLORE.EXE";

            foreach (var pair in browserNameDict)
            {
                if (defaultName.ToUpper().Contains(pair.Value))
                {
                    return pair.Key;
                }
            }

            return BrowserNames.Unknown;
        }

        /// <summary>
        /// Get version for default browser
        /// </summary>
        /// <returns></returns>
        public static string GetDefaultBrowserVersion()
        {
            string browserCommandRegKey = string.Concat(BrowserInstallerRegKey, browserNameDict[GetDefaultBrowserName()], @"\shell\open\command");
            RegistryKey key = Registry.LocalMachine.OpenSubKey(browserCommandRegKey);

            if (key != null)
            {
                string file = key.GetValue("", "c:\notexist.resource").ToString().Replace("\"", "");

                if (System.IO.File.Exists(file))
                {
                    FileVersionInfo fver = FileVersionInfo.GetVersionInfo(file);
                    Version ver = new Version(fver.ProductVersion);
                    return ver.Major.ToString();
                }
            }

            return "0";
        }

        #endregion
        
        #region os info
        public static string GetSystemArchitecture()
        {
            return System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
        }

        /// <summary>
        /// Get Operating system name
        /// </summary>
        /// <returns>OS name as string. E.g "Windows 7"</returns>
        private static string GetSystemName()
        {
            object fullname = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion").GetValue("ProductName");
            return fullname.ToString();
        }

        private static string GetSystemServicePack()
        {
            return Environment.OSVersion.ServicePack.Split('(')[0].Trim();
        }

        #endregion
    }
}
